package com.wqh.controller;

import com.wqh.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/wav")
public class testController {

    @PostMapping("/enroll")
    public R enrol(MultipartFile[] myfiles,String accessToke,String groupId){
        R r = new R();

        if(myfiles.length < 3){
            r.setCode(500);
            r.setMsg("请至少上传三条语音");
            System.out.println(r);
            return r;
        }
        long size = myfiles.length;
        for (MultipartFile file: myfiles) {
            String originalFilename = file.getOriginalFilename();
            System.out.println(originalFilename);
        }
        r.setCode(200);
        r.setMsg("上传成功");
        System.out.println(r);
        return r;
    }

    @PostMapping("/verify")
    public Map<String, Object> verify(String accessToken, MultipartFile[] myfiles, HttpServletResponse response){

        FileInputStream fileInputStream = null;
        ServletOutputStream outputStream = null;
        Map<String,Object> map = new HashMap<>();
        if(myfiles.length < 3){
            map.put("code",500);
            map.put("result",false);
            map.put("score",0);
            map.put("msg","至少要上传三个语音文件");
            OutFileTxt(map);
            return map;
        }
        long size = myfiles.length;
        for (MultipartFile file: myfiles) {
            String originalFilename = file.getOriginalFilename();
            System.out.println(originalFilename);
        }
        map.put("code",200);
        map.put("result",true);
        map.put("score",4892);
        //输出txt
        OutFileTxt(map);
        System.out.println(map);

        return map;
    }



    private void OutFileTxt(Map map){
        try {
            String line = System.getProperty("line.separator");
            FileWriter fileWriter = new FileWriter("C:\\Users\\15622\\Desktop\\面试\\result.txt",true);
            StringBuffer stringBuffer = new StringBuffer();
            Set set = map.entrySet();
            Iterator iterator = set.iterator();
            while (iterator.hasNext()){
                Map.Entry entry = (Map.Entry) iterator.next();
                stringBuffer.append(entry.getKey()+":"+entry.getValue()).append(line);
            }
            fileWriter.write(stringBuffer.toString());
            fileWriter.close();
            //Integer
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
